﻿using System;
using System.Collections.Generic;
using wojilu.Apps.Qycms.Domain;
using wojilu.Members.Interface;
using wojilu.Members.Sites.Domain;
using wojilu.Members.Sites.Service;
using wojilu.Members.Users.Domain;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;

namespace wojilu.Web.Controller.Admin.Qycms
{
    public class AdvertController : BaseController
    {
        public virtual IAdminLogService<SiteLog> logService { get; set; }

        public AdvertController()
        {
            logService = new SiteLogService();
        }

        private void setpublic()
        {
            User user = ctx.viewer.obj as User;
            var modelPowerGroup = PowerGroup.findById(user.GroupId);
            bind("powergroup", modelPowerGroup);
        }

        [HttpGet, HttpPost]
        public virtual void AdvertTypeList()
        {
            SetPublic();
            setpublic();
            set("load", to(new AdminController().Load) + "?");
            set("view", to(new QycmsController().Admin_Tab_List));
            var condition = " lng='" + DefaultLng + "'";
            DataPage<Advert_Type> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            var isclass = ctx.GetInt("isclass");
            switch (isclass)
            {
                case 1:
                    condition += " and isclass = 1 ";
                    break;

                case 2:
                    condition += " and isclass = 0 ";
                    break;
            }

            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "id";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "desc";
            condition += " order by " + limitkey + " " + limitclass;
            CurrentRequest.setCurrentPage(page_id);
            list = Advert_Type.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

        [HttpPost]
        public virtual void AdvertTypeDel()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            if (db.deleteBatch<Advert_Type>(condition) > 0)
            {
                logService.Add((User)ctx.viewer.obj, lang("adverttypemain_del_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("adverttypemain_del_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void AdvertTypeSetting()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            var value = ctx.Post("value");
            var dbname = ctx.Post("dbname");
            db.updateBatch<Advert_Type>("set " + dbname + "=" + value, condition);
            if (!ctx.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("adverttypemain_setting_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("adverttypemain_setting_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void AdvertTypeAdd()
        {
            SetPublic();
            setpublic();
            target(AdvertTypeAddDo);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("refalse", 1);
            set("tab", "true");
            var lng = DefaultLng;
            set("lng", lng);
        }

        [HttpPost]
        public virtual void AdvertTypeAddDo()
        {
            Advert_Type a = new Advert_Type();
            a.lng = ctx.Post("lng");
            a.adtypename = ctx.Post("adtypename");
            a.content = ctx.PostHtmlAll("content");
            a.width = ctx.PostInt("width");
            a.height = ctx.PostInt("height");
            a.isclass = 1;
            a.iscode = 0;
            a.isxml = ctx.PostInt("isxml");
            a.xmltemplatefile = ctx.Post("xmltemplatefile");
            a.xmlfile = ctx.Post("xmlfile");
            a.xmlpath = ctx.Post("xmlpath");
            Result result = a.insert();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("adverttypemain_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("adverttypemain_add_log") + lang("log_extra_ok"), "{Id:" + a.Id + ", Name:'" + a.adtypename + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void AdvertTypeEdit(Int64 Id)
        {
            SetPublic();
            setpublic();
            target(AdvertTypeEditDo, Id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("type", "edit");
            Advert_Type a = Advert_Type.findById(Id);
            bind("read", a);
        }

        [HttpPost]
        public virtual void AdvertTypeEditDo(Int64 Id)
        {
            Advert_Type a = Advert_Type.findById(Id);
            a.adtypename = ctx.Post("adtypename");
            a.content = ctx.PostHtmlAll("content");
            a.width = ctx.PostInt("width");
            a.height = ctx.PostInt("height");
            a.isxml = ctx.PostInt("isxml");
            a.xmltemplatefile = ctx.Post("xmltemplatefile");
            a.xmlfile = ctx.Post("xmlfile");
            a.xmlpath = ctx.Post("xmlpath");
            Result result = a.update();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("adverttypemain_edit_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("adverttypemain_edit_log") + lang("log_extra_ok"), "{Id:" + a.Id + ", Name:'" + a.adtypename + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet, HttpPost]
        public virtual void AdvertList(Int64 id)
        {
            SetPublic();
            setpublic();
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");

            var condition = " lng='" + DefaultLng + "'";
            DataPage<Advert> list = null;
            Int32 maxperpage = ctx.PostInt("MaxPerPage");
            Int32 page_id = ctx.PostInt("page_id") + 1;
            String limitkey = ctx.Get("limitkey");
            String limitclass = ctx.Get("limitclass");
            var isclass = ctx.GetInt("isclass");
            var istime = ctx.GetInt("istime");
            if (id > 0)
            {
                condition += " and atid=" + id;
            }
            switch (isclass)
            {
                case 1:
                    condition += " and isclass = 1 ";
                    break;

                case 2:
                    condition += " and isclass = 0 ";
                    break;
            }
            switch (istime)
            {
                case 1:
                    condition += " and istime = 1 ";
                    break;

                case 2:
                    condition += " and istime = 0 ";
                    break;
            }

            if (strUtil.IsNullOrEmpty(limitkey)) limitkey = "id";
            if (strUtil.IsNullOrEmpty(limitclass)) limitclass = "desc";
            condition += " order by " + limitkey + " " + limitclass;
            CurrentRequest.setCurrentPage(page_id);
            list = Advert.findPage(condition, maxperpage);
            if (ctx.web.postValueAll().Count == 0)
            {
                ctx.web.ResponseWrite(list.RecordCount.ToString());
                ctx.web.ResponseEnd();
            }
            bind("list", list.Results);
        }

        [HttpGet]
        public virtual void XmlCreat(Int64 id)
        {

        }

        [HttpPost]
        public virtual void AdvertDel()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            if (db.deleteBatch<Advert>(condition) > 0)
            {
                logService.Add((User)ctx.viewer.obj, lang("advertmain_del_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("advertmain_del_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void AdvertSort()
        {
            var infoid = ctx.PostIdList("infoid");
            var pid = ctx.PostIdList("pid");
            var strArray = infoid.Split(new char[] { ',' });
            var strArray1 = pid.Split(new char[] { ',' });
            for (int i = 0; i < strArray.Length; i++)
            {
                string condition = string.Format("Id = {0}", strArray[i]);
                db.updateBatch<Advert>("set pid=" + strArray1[i], condition);
            }
            logService.Add((User)ctx.viewer.obj, lang("advertmain_sort_log") + lang("log_extra_ok"), "{Id:" + infoid + "'}", "", ctx.Ip);
            ctx.web.ResponseWrite("true");
            ctx.web.ResponseEnd();
        }

        [HttpPost]
        public virtual void AdvertSetting()
        {
            string Ids = ctx.PostIdList("selectinfoid");
            string condition = string.Format("Id in ({0})", Ids);
            var value = ctx.Post("value");
            var dbname = ctx.Post("dbname");
            db.updateBatch<Advert>("set " + dbname + "=" + value, condition);
            if (!ctx.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("advertmain_setting_log") + lang("log_extra_ok"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("advertmain_setting_log") + lang("log_extra_no"), "{Id:" + Ids + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void AdvertAdd(Int64 id)
        {
            SetPublic();
            setpublic();
            target(AdvertAddDo, id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("refalse", 1);
            set("tab", "true");
            var lng = DefaultLng;
            set("lng", lng);
            var list = Advert_Type.find("lng=:l").set("l", lng).list();
            bind("typelist", list);
            var adlist = cdb.findAll<AdType>();
            bind("adlist", adlist);
        }

        [HttpPost]
        public virtual void AdvertAddDo(Int64 id)
        {
            Advert a = new Advert();
            a.pid = 50;
            a.atid = id;
            a.lng = ctx.Post("lng");
            a.title = ctx.Post("title");
            a.url = ctx.Post("url");
            a.adtype = ctx.PostInt("adtype");
            var filename = ctx.Post("filename");
            if (!filename.StrIsNullOrEmpty() && filename.StartsWith("../"))
            {
                filename = Right(filename, filename.Length - 3);
            }
            a.filename = filename;
            a.click = 0;
            a.istime = ctx.PostInt("istime");
            a.starttime = TypeCon.CDate(ctx.Post("starttime"));
            a.endtime = TypeCon.CDate(ctx.Post("endtime"));
            a.addtime = DateTime.Now;
            a.isclass = 1;
            a.content = ctx.Post("content");
            a.islink = ctx.PostInt("islink") == 2 ? 0 : 1;
            a.gotoid = ctx.PostInt("gotoid");
            Result result = a.insert();
            if (result.HasErrors)
            {
                logService.Add((User)ctx.viewer.obj, lang("advertmain_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("false");
            }
            else
            {
                logService.Add((User)ctx.viewer.obj, lang("advertmain_add_log") + lang("log_extra_ok"), "{Id:" + a.Id + ", Name:'" + a.title + "'}", "", ctx.Ip);
                ctx.web.ResponseWrite("true");
            }
            ctx.web.ResponseEnd();
        }

        [HttpGet]
        public virtual void AdvertEdit(Int64 id)
        {
            SetPublic();
            setpublic();
            target(AdvertEditDo, id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            set("type", "edit");
            var lng = DefaultLng;
            set("lng", lng);
            Advert a = Advert.findById(id);
            bind("read", a);
            List<Advert_Type> at = Advert_Type.find("lng=:l").set("l", lng).list();
            bind("list", at);
            var adlist = cdb.findAll<AdType>();
            bind("adlist", adlist);
        }

        [HttpPost]
        public virtual void AdvertEditDo(Int64 id)
        {
            Advert a = Advert.findById(id);
            a.lng = ctx.Post("lng");
            a.atid = ctx.PostInt("atid");
            a.title = ctx.Post("title");
            a.url = ctx.Post("url");
            a.adtype = ctx.PostInt("adtype");
            a.istime = ctx.PostInt("istime");
            a.starttime = TypeCon.CDate(ctx.Post("starttime"));
            a.endtime = TypeCon.CDate(ctx.Post("endtime"));
            var filename = ctx.Post("filename");
            if (!filename.StrIsNullOrEmpty() && filename.StartsWith("../"))
            {
                filename = Right(filename, filename.Length - 3);
            }
            a.filename = filename;
            a.content = ctx.Post("content");
            a.islink = ctx.PostInt("islink") == 2 ? 0 : 1;
            a.gotoid = ctx.PostInt("gotoid");
            var inputclass = ctx.Post("inputclass");
            if (inputclass == "add")
            {
                Result result = a.insert();
                if (result.HasErrors)
                {
                    logService.Add((User)ctx.viewer.obj, lang("advertmain_add_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                    ctx.web.ResponseWrite("false");
                }
                else
                {
                    logService.Add((User)ctx.viewer.obj, lang("advertmain_add_log") + lang("log_extra_ok"), "{Id:" + a.Id + ", Name:'" + a.title + "'}", "", ctx.Ip);
                    ctx.web.ResponseWrite("true");
                }
                ctx.web.ResponseEnd();
            }
            else
            {
                Result result = a.update();
                if (result.HasErrors)
                {
                    logService.Add((User)ctx.viewer.obj, lang("adverttypemain_edit_log") + lang("log_extra_no"), "{Erros:'" + result.ErrorsText + "'}", "", ctx.Ip);
                    ctx.web.ResponseWrite("false");
                }
                else
                {
                    logService.Add((User)ctx.viewer.obj, lang("adverttypemain_edit_log") + lang("log_extra_ok"), "{Id:" + a.Id + ", Name:'" + a.title + "'}", "", ctx.Ip);
                    ctx.web.ResponseWrite("true");
                }
                ctx.web.ResponseEnd();
            }
        }

        [HttpGet]
        public virtual void AdvertCopy(Int64 id)
        {
            SetPublic();
            setpublic();
            target(AdvertEditDo, id);
            set("iframename", ctx.Get("iframename"));
            set("iframeheightwindow", ctx.GetInt("iframeheightwindow"));
            set("load", to(new AdminController().Load) + "?");
            var type = ctx.Get("type");
            set("type", "add");
            var lng = DefaultLng;
            set("lng", lng);
            Advert a = Advert.findById(id);
            bind("read", a);
            List<Advert_Type> at = Advert_Type.find("lng=:l").set("l", lng).list();
            bind("list", at);
            var adlist = cdb.findAll<AdType>();
            bind("adlist", adlist);
        }

    }
}